using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using System.Web;

namespace Framework.Library.Utilities
{
	public class MultimediaUtility
	{
		private static bool ThumbnailCallback()
		{
			return false;
		}
        
	    public static Image Crop(Image p, int width, int height)
	    {
	        Bitmap b = new Bitmap(p);
            Bitmap imgCropped = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            Graphics objGraphic = Graphics.FromImage(imgCropped);
            objGraphic.Clear(Color.White);
            int intStartTop = 0;
            int intStartLeft = 0;
            objGraphic.DrawImage(b, intStartLeft, intStartTop);
	        b.Dispose();
            objGraphic.Dispose();
	        return imgCropped;
	    }
       
		

        #region  set and get Thumbnail with List
        public static bool SetThumbnailList(string filePath, string newPath, string avatarPath, int width, int height)
        {
            if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
            FileInfo fileInfo = new FileInfo(filePath);
            Image orImgeCrop = Crop(Image.FromFile(avatarPath + fileInfo.Name), width, height);
            orImgeCrop.Save(newPath + fileInfo.Name);
            orImgeCrop.Dispose();
            return true;
        }
        public static bool SetAvatarListThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists) return false;
            return SetThumbnailList(filePath, fileInfo.Directory + "\\ListAvatar\\", fileInfo.Directory + "\\Avatar\\", iThumbWidth, iThumbWidth);
        }
        public static string GetListAvatar(string avatar)
        {
            int splitIndex = avatar.LastIndexOf("/");
            if (splitIndex != -1)
                return avatar.Substring(0, splitIndex) + "/ListAvatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            else return string.Empty;

        }
        #endregion

        #region set and get Thumbnail with Special
        public static bool SetThumbnailSpecial(string filePath, string newPath, string avatarPath, int width, int height)
        {
            if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
            if(!Directory.Exists(avatarPath)) Directory.CreateDirectory(avatarPath);
            FileInfo fileInfo = new FileInfo(filePath);
            Image orImgeCrop = Crop(Image.FromFile(avatarPath + fileInfo.Name), width, height);
            orImgeCrop.Save(newPath + fileInfo.Name);
            orImgeCrop.Dispose();
            return true;
        }
        public static bool SetAvatarSpecialThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            //FileInfo fileInfo = new FileInfo(filePath);
            //if (!fileInfo.Exists) return false;
            //if (SetThumbnail(filePath, fileInfo.Directory + "\\SpecialAvatar\\Avatar\\", iThumbWidth, 0))
            //    return SetThumbnailSpecial(filePath, fileInfo.Directory + "\\SpecialAvatar\\", fileInfo.Directory + "\\SpecialAvatar\\Avatar\\", iThumbWidth, iThumbHeight);
            //else
            //    return false;

            //FileInfo fileInfo = new FileInfo(filePath);
            //if (!fileInfo.Exists) return false;
            //return SetThumbnail(filePath, fileInfo.Directory + "\\SpecialAvatar\\", iThumbWidth, iThumbHeight);
            return true;
            
        }
        public static string GetSpecialAvatar(string avatar)
        {
            return avatar;
            //int splitIndex = avatar.LastIndexOf("/");
            //if (splitIndex != -1)
            //    //return avatar.Substring(0, splitIndex) + "/SpecialAvatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            //    return avatar.Substring(0, splitIndex) + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            //else return string.Empty;

        }
        #endregion
        
        #region set and get Thumbnail
        public static bool SetThumbnail(string filePath, string newPath, int iThumbWidth, int iThumbHeight)
        {
            if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
            FileInfo fileInfo = new FileInfo(filePath);
            Image orImg = Image.FromFile(fileInfo.FullName);
            if ((iThumbHeight == 0) && (iThumbWidth == 0)) return false;
            else if ((iThumbHeight != 0) && (iThumbWidth == 0))
                iThumbWidth = (int)(iThumbHeight * orImg.Width) / orImg.Height;
            else if ((iThumbHeight == 0) && (iThumbWidth != 0))
                iThumbHeight = (int)(iThumbWidth * orImg.Height) / orImg.Width;

            Bitmap myBitmap = new Bitmap(iThumbWidth, iThumbHeight, PixelFormat.Format24bppRgb);
            myBitmap.SetResolution(orImg.HorizontalResolution, orImg.VerticalResolution);

            Graphics grImage = Graphics.FromImage(myBitmap);
            grImage.Clear(Color.White);
            grImage.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grImage.DrawImage(orImg, new Rectangle(0, 0, iThumbWidth, iThumbHeight), new Rectangle(0, 0, orImg.Width, orImg.Height), GraphicsUnit.Pixel);
            grImage.Dispose();

            if (System.IO.File.Exists(newPath + fileInfo.Name))
            {
                System.IO.File.Delete(newPath + fileInfo.Name);
            }

            myBitmap.Save(newPath + fileInfo.Name);

            return true;
        }
		public static bool SetAvatarThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
		{
			FileInfo fileInfo = new FileInfo(filePath);
			if (!fileInfo.Exists) return false;
			return SetThumbnail(filePath, fileInfo.Directory + "\\Avatar\\", iThumbWidth, 0);
		}
        public static string GetAvatar(string avatar)
        {
            int splitIndex = avatar.LastIndexOf("/");
            if (splitIndex != -1)
                return avatar.Substring(0, splitIndex) + "/Avatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            else return string.Empty;

        }
        #endregion

        #region get and set Thumbnail with Gallery
        public static bool SetAvatarGalleryThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(filePath));
            if (!fileInfo.Exists) return false;
            SetThumbnail(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\AvatarThumbnail\\", 75, 0);
            return SetThumbnailList(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\Avatar\\", fileInfo.Directory.ToString() + "\\AvatarThumbnail\\", iThumbWidth, iThumbWidth);
            //else
            //    return false;
        }

        public static string GetGalleryAvatar(string avatar)
        {
            int splitIndex = avatar.LastIndexOf("/");
            if (splitIndex != -1)
                return avatar.Substring(0, splitIndex) + "/Avatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            else return string.Empty;

        }
        #endregion

        #region get and set Thumbnail with Picture
        public static bool SetAvatarPictureThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(filePath));
            if (!fileInfo.Exists) return false;
            SetThumbnail(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\Avatar\\", 75, 0);
            return SetThumbnailList(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\ListAvatar\\", fileInfo.Directory.ToString() + "\\Avatar\\", iThumbWidth, iThumbWidth);
            //else
            //    return false;
        }

        public static string GetPictureAvatar(string avatar)
        {
            int splitIndex = avatar.LastIndexOf("/");
            if (splitIndex != -1)
                return avatar.Substring(0, splitIndex) + "/Avatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            else return string.Empty;

        }
        #endregion

        #region Get and set thumbnail with Event
        public static bool SetAvatarEventThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists) return false;

            return SetThumbnailList(filePath, fileInfo.Directory + "\\Avatar\\", fileInfo.Directory.ToString() + "/", iThumbWidth, iThumbWidth);
        }
        public static string GetEventAvatar(string avatar)
		{
			int splitIndex = avatar.LastIndexOf("/");
			if (splitIndex != -1)
				return avatar.Substring(0, splitIndex) + "/Avatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
			else return string.Empty;

        }
        #endregion

        #region Get and set thumbnail with Bonus
        public static bool SetBonusThumbnail(string filePath, int iThumbWidth, int iThumbHeight)
        {
            FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(filePath));
            if (!fileInfo.Exists) return false;
            SetThumbnail(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\AvatarThumbnail\\", 75, 0);
            return SetThumbnailList(HttpContext.Current.Server.MapPath(filePath), fileInfo.Directory + "\\Avatar\\", fileInfo.Directory + "\\AvatarThumbnail\\", iThumbWidth, iThumbWidth);
        }
        public static string GetBonusAvatar(string avatar)
        {
            int splitIndex = avatar.LastIndexOf("/");
            if (splitIndex != -1)
                return avatar.Substring(0, splitIndex) + "/Avatar" + avatar.Substring(splitIndex, avatar.Length - splitIndex);
            else return string.Empty;

        }
        #endregion


        public static string GetRealPath(string avatar)
        {
            return avatar;

        }
		public static string strInitImage(string image, int width, int height)
		{
			string retVal = "<img src=\"" + image + "\"";
			if (width > 0) retVal += " width=\"" + width + "px\" ";
			if (height > 0) retVal += " height=\"" + height + "px\" ";
			retVal += ">";
			return retVal;
		}
        public static string strInitFlashWithActiveContent(string fileName, int width, int height, string _align)
        {
            string retVal = "<script type=\"text/javascript\">";
            retVal += "AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0','width','" + width + "','height','" + height + "','id','" + fileName + "','align','" + _align + "','src','" + fileName + "','quality','high','bgcolor','','name','" + fileName + "','allowscriptaccess','sameDomain','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','" + fileName + "' );";
            retVal += "</script>";
            return retVal;
        }
        public static string strInitFlash(string flashURL, int width, int height)
        {
            string retVal = "<EMBED align=baseline src='" + flashURL + "'";
            if (width != 0) retVal += " width=" + width;
            if (height != 0) retVal += " height=" + height;

            retVal += " type=audio/x-pn-realaudio-plugin autostart=\"true\" controls=\"ControlPanel\" console=\"Clip1\" border=\"0\">";
            return retVal;
        }

		

		public static string strInitMultimedia(string mediaPath, int width, int height)
		{
			string retVal = "<EMBED pluginspage='http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/' ";
			if (width != 0) retVal += " width=" + width;
			if (height != 0) retVal += " height=" + height;

			retVal += " src='" + mediaPath + "' type='application/x-mplayer2' ShowStatusBar='1' AutoStart='true' ShowControls='1'></embed>";
			return retVal;
		}
	}
}